package com.shwread.android.ui.widget;

import android.content.Context;
import android.graphics.Paint;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.shwread.android.qysw.activity.ReadingBookActivity;
import com.shwread.android.qysw10000227.R;

import org.geometerplus.fbreader.book.Bookmark;
import org.geometerplus.zlibrary.ui.android.view.LQReadingView;

import logic.util.Util;
import logic.util.logutil.LogUtil;


public class ShowBookNotePOPUP
        implements
        OnClickListener
{
	//图片背景。9图宽
	private static final int         TEXT_BG_WIDTH               = 122;
	// 图片.9问题 可填写区域距离边缘的距离24pt
	private static final int         UNEDIT_WIDTH                = 24 * 2;
	//。9图片右侧白色边缘线距离图片右端的距离  用于限定最大leftMargin
	private static final int         WHITE_LINE_TO_BG_RIGHT_SIDE = 14;
	private static ShowBookNotePOPUP instance;
	private static  int         Padding_Width      ;
	private volatile PopupWindow     popupWindow;
	private Context                  context;
	private View                     contentView;
	private TextView                 tvNoteText;
	private ImageView                ivUpArrow;
	private ImageView                ivDownArrow;
	private ScrollView               scrollView;
	private Bookmark                 bookmark;
	private int                      screenWidth;
	private int                      screenHeight;
	
	private ShowBookNotePOPUP(Context context) {
		contentView = LayoutInflater.from(context).inflate(R.layout.page_popup_container, null);
		init(context, contentView);
	}
	
	public static ShowBookNotePOPUP getInstance(Context context, Bookmark bookmark) {
		if (instance == null) instance = new ShowBookNotePOPUP(context);
		instance.context=context;
		instance.bookmark = bookmark;
		Padding_Width = (int) context.getResources().getDimension(R.dimen.note_text_padding_width)*2;
		return instance;
	}
	
	private void init(Context context, View contentView) {
		this.context = context;
		DisplayMetrics dm = context.getApplicationContext().getResources().getDisplayMetrics();
		screenWidth = dm.widthPixels;
		screenHeight = dm.heightPixels;
		popupWindow = new PopupWindow(contentView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
		popupWindow.setBackgroundDrawable(context.getResources().getDrawable(R.color.common_transparent));
		popupWindow.setOutsideTouchable(true);// 设置pWindow之外的布局可点击
		popupWindow.setTouchInterceptor(new OnTouchListener()
		{
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				
				if (event.getAction() == MotionEvent.ACTION_OUTSIDE)
				{
					popupWindow.dismiss();
					return true;
				}
				return false;
			}
		});
		initView(contentView);
		addListener();
		popupWindow.setAnimationStyle(android.R.style.Animation_Toast);
	}
	
	private void addListener() {
		tvNoteText.setOnClickListener(this);
	}
	
	private void initView(View parentView) {
		tvNoteText = (TextView) parentView.findViewById(R.id.note_popup_content);
		ivUpArrow = (ImageView) parentView.findViewById(R.id.note_popup_note_uparrow);
		ivDownArrow = (ImageView) parentView.findViewById(R.id.note_popup_note_downarrow);
		scrollView = (ScrollView) parentView.findViewById(R.id.note_popup_scroll);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.note_popup_content: 
				((ReadingBookActivity) context).changeBooKRemark(bookmark);
				popupWindow.dismiss();
				break;
		}
	}
	
	public void show2(View viewParent, int x, int y) {
		LogUtil.d("luhuhai", "show   [" + x + "," + y + "]");
		Paint paint = tvNoteText.getPaint();
		float len = paint.measureText(bookmark.getBookNote().trim());
		// 图片.9问题 可填写区域距离边缘的距离24pt两边
		len += UNEDIT_WIDTH+Padding_Width;
		//.9图片最小长度122
		len = Math.max(len, TEXT_BG_WIDTH);
		int tvWidth = Math.min((int) len, screenWidth);
		int showX = x < screenWidth / 2 ? Math.max(x - tvWidth / 2, 0) : Math.max(screenWidth - (x + tvWidth / 2), 0);
		int showY = 0;
		popupWindow.setWidth(tvWidth < screenWidth ? tvWidth : WindowManager.LayoutParams.FILL_PARENT);
		tvNoteText.setText(bookmark.getBookNote());
		RelativeLayout.LayoutParams layoutParamsScrllView = (RelativeLayout.LayoutParams) scrollView.getLayoutParams();
		if (y < screenHeight / 2)
		{
			//处于上半屏幕，显示在图标下面
			ivUpArrow.setVisibility(View.VISIBLE);
			ivDownArrow.setVisibility(View.GONE);
			showY = y + LQReadingView.ICON_HEIGHT;
			RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) ivUpArrow.getLayoutParams();
			if (x < screenWidth / 2)
			{
				layoutParams.leftMargin = x - showX - Util.getBitMapWidth(R.drawable.arrow_up, context) / 2;
				layoutParams.rightMargin = 0;
				layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
				layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
				ivUpArrow.setLayoutParams(layoutParams);
				
				layoutParamsScrllView.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
				layoutParamsScrllView.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
				scrollView.setLayoutParams(layoutParamsScrllView);
				popupWindow.showAtLocation(viewParent, Gravity.TOP | Gravity.LEFT, showX, showY);
			}
			else
			{
				layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
				layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
				int minRightMargin = WHITE_LINE_TO_BG_RIGHT_SIDE;
				layoutParams.leftMargin = 0;
				layoutParams.rightMargin = Math.max(screenWidth - x - showX - Util.getBitMapWidth(R.drawable.arrow_up, context) / 2, minRightMargin);
				ivUpArrow.setLayoutParams(layoutParams);
				
				layoutParamsScrllView.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
				layoutParamsScrllView.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
				scrollView.setLayoutParams(layoutParamsScrllView);
				popupWindow.showAtLocation(viewParent, Gravity.TOP | Gravity.RIGHT, showX, showY);
			}
		}
		else
		{
			//处于下半屏幕，显示在图标上面
			ivUpArrow.setVisibility(View.GONE);
			ivDownArrow.setVisibility(View.VISIBLE);
			RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) ivDownArrow.getLayoutParams();
			if (x < screenWidth / 2)
			{
				layoutParams.leftMargin = x - showX - Util.getBitMapWidth(R.drawable.arrow_down, context) / 2;
				layoutParams.rightMargin = 0;
				layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
				layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
				ivDownArrow.setLayoutParams(layoutParams);
				layoutParamsScrllView.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
				layoutParamsScrllView.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
				scrollView.setLayoutParams(layoutParamsScrllView);
				popupWindow.showAtLocation(viewParent, Gravity.BOTTOM | Gravity.LEFT, showX, screenHeight - y);
			}
			else
			{
				layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
				layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
				int minRightMargin = WHITE_LINE_TO_BG_RIGHT_SIDE;
				layoutParams.leftMargin = 0;
				layoutParams.rightMargin = Math.max(screenWidth - x - showX - Util.getBitMapWidth(R.drawable.arrow_down, context) / 2, minRightMargin);
				ivDownArrow.setLayoutParams(layoutParams);
				
				layoutParamsScrllView.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
				layoutParamsScrllView.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
				scrollView.setLayoutParams(layoutParamsScrllView);
				popupWindow.showAtLocation(viewParent, Gravity.BOTTOM | Gravity.RIGHT, showX, screenHeight - y);
			}
		}
		
	}
}
